<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>京东商品图片</title>
    <style>
        *{
            margin:0;padding:0;
        }
        .container{
            width: 400px; min-height:400px;
            margin: 100px auto;
        }
        img{
            width: 100%;
        }
        
        .item{
            box-sizing: border-box;
            width: 20%;
            float: left;
            padding: 10px;
        }

        .item img{
            padding: 5px;
            border:solid 1px #ccc;
        }
        .small-images{
            overflow: hidden;
        }
        .small-images .active{
            border:solid 1px rgb(46, 46, 141);
        }
    </style>
</head>

<body>
    <div id="root">
               
    </div>
    <script src="./js/react.development.js"></script>
    <script src="./js/react-dom.development.js"></script>
    <script src="./js/babel.min.js"></script>
    <script type="text/babel">
        //声明类式组件
        class Goods extends React.Component{
            //声明状态
            state = {
                //当前正在显示图片的下标
                index: 0,
                images: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg',
                ]
            }

            render(){
                //解构赋值  this.state.index  this.state.images
                let {index, images} = this.state;

                return <div className="container">
                            <div className="big-image">
                                <img src={images[index]} alt="" />
                            </div>
                            <div className="small-images">
                                {
                                    this.state.images.map((item, i) => {
                                        return <div onClick={this.click} key={i} className="item">
                                            <img aid={i} className={i === index ? 'active' : ''} src={item} alt="" />
                                        </div>
                                    })
                                }
                            </div>
                        </div> 
            }

            click = (e) => {
                //获取事件源中小图的 src 路径
                // console.log(e.target);
                let id = Number(e.target.getAttribute('aid')); 

                //修改 state 中 src 中的值
                this.setState({
                    index: id
                })
            }
        }

        //渲染
        ReactDOM.render(<Goods />, document.querySelector('#root'));
    </script>
</body>

</html>